package com.situ.company.employee.servlet;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.fileupload.FileUploadException;

import com.situ.company.employee.model.EmployeeModel;
import com.situ.company.employee.service.EmployeeService;
import com.situ.company.util.FmtRequest;
import com.situ.company.util.UpLoad;

@WebServlet("/EmployeeUploadServlt")
public class EmployeeUploadServlt extends HttpServlet {
	private static final long serialVersionUID = 1L;
	
	private EmployeeService service = new EmployeeService();
       
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
	    doPost(req, resp);
	}

	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
	    Map<String, Object> map = null;
	    try {
            map = UpLoad.upload(req);
        } catch (FileUploadException | IOException e) {
            e.printStackTrace();
        }
	    
	    List<String> list = (List<String>) map.get("list");
	    String image = list.get(0);
	    String code = (String) map.get("code");
	    
	    EmployeeModel model = new EmployeeModel(code);
	    model.setImage(image);
	    String res = service.update(model);
	    
	    Map<String, String> result = new HashMap<String, String>();
	    result.put("code", res);
	    result.put("image", image);
	    FmtRequest.write(resp.getWriter(), result);
	}
}
